/*
 * TimeUtil.cpp
 *
 *  Created on: 2018年7月15日
 *      Author: zhengboyuan
 */

#include "TimeUtil.h"
#include "DateTime.h"
#include "TStringCast.h"


namespace util
{

TimeUtil::TimeUtil()
{
}

TimeUtil::~TimeUtil()
{
}

std::string TimeUtil::format(time_t t)
{
    std::string str;
    if (t > 0)
    {
        comn::DateTime dt(t);
        str = dt.format("%Y-%m-%d %H:%M:%S");
    }
    return str;
}

std::string TimeUtil::format(time_t t, int ms)
{
    std::string str;
    if (t > 0)
    {
        comn::DateTime dt(t, ms);
        str = dt.format("%Y-%m-%d %H:%M:%S");
        str += '.';
        str += comn::StringCast::toString(ms);
    }
    return str;
}

bool TimeUtil::parse(const std::string& str, time_t& t)
{
    int nYear = 0;
    int nMonth = 0;
    int nDay = 0;
    int nHour = 0;
    int nMin = 0;
    int nSec = 0;
    sscanf(str.c_str(), "%d-%d-%d %d:%d:%d", &nYear, &nMonth, &nDay, &nHour, &nMin, &nSec);

    comn::DateTime dt(nYear, nMonth, nDay, nHour, nMin, nSec, 0);
    t = dt.totalSecond();

    return true;
}

time_t TimeUtil::toMinute(time_t t)
{
    return (t / 60) * 60;
}

time_t TimeUtil::toHour(time_t t)
{
    return (t / 3600) * 3600;
}

time_t TimeUtil::toDay(time_t t)
{
    comn::DateTime dt(t);
    comn::DateTime date(dt.getYear(), dt.getMonth(), dt.getDay(), 0, 0, 0, 0);
    return date.totalSecond();
}

time_t TimeUtil::toMonth(time_t t)
{
    comn::DateTime dt(t);
    comn::DateTime date(dt.getYear(), dt.getMonth(), 1, 0, 0, 0, 0);
    return date.totalSecond();
}

time_t TimeUtil::addMonth(time_t t, int months)
{
    comn::DateTime dt(t);

    int year = dt.getYear();
    int month = dt.getMonth() + months;
    year += (month - 1) / 12;
    month = (month - 1) % 12 + 1;

    comn::DateTime date(year, month, dt.getDay(), dt.getHour(), dt.getMinute(), dt.getSecond(), 0);
    return date.totalSecond();
}

time_t TimeUtil::toYear(time_t t)
{
    comn::DateTime dt(t);
    comn::DateTime date(dt.getYear(), 1, 1, 0, 0, 0, 0);
    return date.totalSecond();
}

time_t TimeUtil::addYear(time_t t, int years)
{
    comn::DateTime dt(t);
    comn::DateTime date(dt.getYear() + years, dt.getMonth(), dt.getDay(), dt.getHour(), dt.getMinute(), dt.getSecond(), 0);
    return date.totalSecond();
}

time_t TimeUtil::toWeekDay(time_t t)
{
	comn::DateTime dt(t);
	int wday = dt.getWeekday();
	comn::DateTime date(dt.getYear(), dt.getMonth(), dt.getDay(), 0, 0, 0, 0);
	return addDay(date.totalSecond(), -wday);
}

time_t TimeUtil::toNextWeekDay(time_t t)
{
	comn::DateTime dt(t);
	int wday = dt.getWeekday();
	comn::DateTime date(dt.getYear(), dt.getMonth(), dt.getDay(), 0, 0, 0, 0);
	return addDay(date.totalSecond(), 7 - wday);
}

time_t TimeUtil::addDay(time_t t, int days)
{
	return t + days * 24 * 3600;
}

time_t TimeUtil::toQuarter(time_t t)
{
    comn::DateTime dt(t);
    int month = dt.getMonth();
    month = (((month + 2) / 3) - 1) * 3 + 1;

    comn::DateTime date(dt.getYear(), month, 1, 0, 0, 0, 0);
    return date.totalSecond();
}

time_t TimeUtil::addQuarter(time_t t, int count)
{
    comn::DateTime dt(t);
    int month = dt.getMonth();
    month = (((month + 2) / 3) - 1) * 3 + 1;

    int year = dt.getYear();
    month += count * 3;
    year += (month - 1) / 12;
    month = (month - 1) % 12 + 1;

    comn::DateTime date(year, month, dt.getDay(), dt.getHour(), dt.getMinute(), dt.getSecond(), 0);
    return date.totalSecond();
}


} /* namespace util */
